<?php

namespace app\api\controller\member;

use think\Controller;
use think\Request;
use app\common\model\User as UserModel;

/**
 * 会员接口基类
 * Class ApiMemberBase
 * @package app\api\controller
 */
class ApiMemberBase extends Controller
{
    protected function _initialize()
    {
        $request = Request::instance();
        $param = $request->param();

        $data = $this->getUserByToken($param['uid'], $param['token']);

        if ($data['code'] != 200) {
            constructRedirect($data);
        }

    }

    private function getUserByToken($uid, $token)
    {
        $user_model = new UserModel();
        $map = [];
        $map['id'] = $uid;
        $map['token'] = $token;
        $user = $user_model->where($map)->find();
        if (!$user) {
            $data['code'] = 401;
            $data['msg'] = '非法操作';
            $data['error_code'] = 10001;
            return $data;
        }
        if ($user->status != 1) {
            $data['code'] = 401;
            $data['msg'] = '账户被禁用';
            $data['error_code'] = 10003;
            return $data;
        }

        $time = $_SERVER['REQUEST_TIME'];
        if ($time > $user->time_out) {
            $data['code'] = 401;
            $data['msg'] = 'token超时';
            $data['error_code'] = 10006;
            return $data;
        }

        $data['code'] = 200;
        $data['msg'] = 'success';
        $data['error_code'] = 0;
        return $data;
    }

}